home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: while loop problem
- Date: Wed, 13 Mar 96 18:06:16 GMT
- Organization: none
- Distribution: world
- Message-ID: <826740376snz@genesis.demon.co.uk>
- References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca> <Pine.A32.3.91.960312145524.69354D-100000@red.weeg.uiowa.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Pine.A32.3.91.960312145524.69354D-100000@red.weeg.uiowa.edu>
- robinson@blue.weeg.uiowa.edu "The Amorphous Mass" writes:
-
- >On Tue, 12 Mar 1996, Bill Simpson wrote:
- >
- >> Consider the following code fragment:
- >>
- >> y=2000; z=1000;
- >> x=0;
- >> while(x<=XMAX)
- >> {
- >> x+=(int) erand(mean);
- >> setdot(x,y,z);
- >> }
- >
- > y = 2000; z = 1000; x = 0;
- >
- > do {
- > setdot(x,y,z);
- > x += (int)erand(mean);
- > } while (x <= XMAX);
-
- Compared to the original this sets an extra dot at x==0. Others have
- 'improved' on this by writing something similar to:
-
- > y = 2000; z = 1000;
- > x = (int)erand(mean);
- >
- > do {
- > setdot(x,y,z);
- > x += (int)erand(mean);
- > } while (x <= XMAX);
-
- but this doesn't test the first value of x so is invalid unless you
- know that (int)erand(mean) <= XMAX is guaranteed is guaranteed (which isn't stated). That
- can be fixed by moving the test to the top i.e.
-
- y = 2000; z = 1000;
- x = (int)erand(mean);
-
- while (x <= XMAX) {
- setdot(x,y,z);
- x += (int)erand(mean);
- }
-
- which can also be written as:
-
- y = 2000; z = 1000;
- x = (int)erand(mean);
-
- for (x = (int)erand(mean); x <= XMAX; x += (int)erand(mean))
- setdot(x,y,z);
-
- Bill's 2nd article also contains a correct version. It can be written more
- compactly as:
-
- y = 2000; z = 1000;
-
- x = 0;
- while ((x += (int)erand(mean)) <= XMAX)
- setdot(x,y,z);
-
- You could write this as a for loop but I prefer it in this form.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-